package com.sljy.hzk.ui.fragment;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.sljy.hzk.R;
import com.sljy.hzk.entity.TaskDetailEntity;
import com.sljy.hzk.ui.fragment.lazyfragment.LazyFragment;
import com.sljy.hzk.util.picture.TakePictureUtils;
import com.sljy.hzk.view.SmoothCheckBox;
import com.sljy.hzk.view.adapter.ChosePicAdapter;
import com.sljy.hzk.view.adapter.TaskDoSubjectAdapter;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created  on 2018/7/3 0003
 *
 * @author 唐开阔
 * @describe 具体的一个任务
 */
public class TaskFragment extends LazyFragment {
    ChosePicAdapter chosePicAdapter;
    private static final int SPAN_COUNT = 3;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.recycer_subject)
    RecyclerView recycerSubject;
    @BindView(R.id.recycer)
    RecyclerView recycerPic;
    @BindView(R.id.switch1)
    Switch box;
    @BindView(R.id.picNum)
    TextView picNum;
    public TakePictureUtils takePictureUtils;
    public TaskDetailEntity taskDetailEntity;

    public static TaskFragment get(TaskDetailEntity taskDetailEntity) {
        TaskFragment taskFragment = new TaskFragment();
        taskFragment.taskDetailEntity = taskDetailEntity;
        Log.d("TaskFragment", ">>>get---" );
        return taskFragment;
    }

    @Override
    protected int getResId() {
        return R.layout.fragment_task;
    }

    @Override
    protected void onRealViewLoaded(View view) {
        tvTitle.setText(taskDetailEntity.getTitle());
        takePictureUtils = TakePictureUtils.Builder.from(this).isCompressor(false).isTailor(false).creat();
        takePictureUtils.setTakeCallBacklistener(new TakePictureUtils.TakePictureListener() {
            @Override public void successful(File outFile) {
                showPic(outFile.getPath());
            }
            @Override public void failed(int errorCode, List<String> deniedPermissions) { }
        });
        List<String> picPath = new ArrayList<String>();
        for(String p : taskDetailEntity.getPicUrl()){
            picPath.add(p.replace("\\","/"));
        }
        chosePicAdapter = new ChosePicAdapter(getContext(), taskDetailEntity.getPicUrl().size() > 0 ? picPath:taskDetailEntity.getPicPaths(),9);
        chosePicAdapter.setListener(() -> showPhotoDialog());
        recycerPic.setLayoutManager(new GridLayoutManager(getContext(), SPAN_COUNT));
        recycerPic.setAdapter(chosePicAdapter);
        recycerSubject.setLayoutManager(new LinearLayoutManager(getActivity()));
        recycerSubject.setAdapter(new TaskDoSubjectAdapter(taskDetailEntity.getList(), getActivity()));
        box.setSwitchTextAppearance(getActivity(),R.style.s_false);
        if(taskDetailEntity.isHaveAqyh())
            box.setChecked(true);
        box.setOnCheckedChangeListener(new Switch.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                taskDetailEntity.setHaveAqyh(isChecked);
                //控制开关字体颜色
                if (isChecked) {
                    box.setSwitchTextAppearance(getActivity(),R.style.s_true);
                }else {
                    box.setSwitchTextAppearance(getActivity(),R.style.s_false);
                }
            }
        });
    }

    public void showPhotoDialog() {
        new MaterialDialog.Builder(getContext())
                .items("选择本地照片", "拍照")
                .itemsCallback((dialog, itemView, position, text) -> {
                    switch (position) {
                        case 0: // 选择本地照片
                            takePictureUtils.getByAlbum();
                            break;
                        case 1: // 拍照
                            takePictureUtils.getByCarema();
                            break;
                    }
                }).show();
    }

    private void showPic(String path) {
        taskDetailEntity.getPicPaths().add(path);
        chosePicAdapter.notifyItemInserted(taskDetailEntity.getPicPaths().size() );
        picNum.setText("添加图片（"+taskDetailEntity.getPicPaths().size()+"/9"+")");
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        takePictureUtils.attachToActivityForResult(requestCode, resultCode, data);

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        takePictureUtils.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

}
